<?php

class Default_Model_Upload
{
	
	const DIR_UPLOAD = '../upload/';
	const DIR_THUMB = '../thumb/';

	public static $mime = array(
		'jpg' => 'image/jpeg',
		'png' => 'image/png',
		'gif' => 'image/gif',
	);
	
	public function flushFile($filename, $type='application/octet-stream')
	{
		$handle = fopen($filename, 'r');
		if ($handle)
		{
			header('Content-Type: '.$type);
			header("Content-Length: ".filesize($filename));
			while (!feof($handle))
			{
				echo fread($handle, 8192);
				flush();
			}
			fclose($handle);
		}
	}

	public function createPath($filename, $path=self::DIR_UPLOAD)
	{
		$part = explode('/', $filename);
		while (count($part) > 1)
		{
			$path .= '/' . array_shift($part);
			if (!is_dir($path))
			{
				mkdir($path);
				chmod($path, 0775);
			}
		}
		$path .= '/' . array_shift($part);
		return $path;
	}

	protected function createImage($width, $height, $type)
	{
		$img = imagecreatetruecolor($width, $height);
		if ($type == IMAGETYPE_PNG)
		{
			$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
			imagefill($img, 0, 0, $color);
			imagealphablending($img, true);
			imagesavealpha($img, true);
		}
		elseif ($type == IMAGETYPE_GIF)
		{
			$color = imagecolorallocatealpha($img, 255, 255, 255, 127);
			imagefill($img, 0, 0, $color);
			imagecolortransparent($img, $color);
		}
		return $img;
	}
	
	public function createThumbnail($sourceFile, $targetFile, $width, $height, $fit)
	{
		ini_set('memory_limit', '128M');
		$result = false;
		$data = @getimagesize($sourceFile);
		if ($data)
		{
			switch ($data[2])
			{
				case IMAGETYPE_JPEG:
					$source = imagecreatefromjpeg($sourceFile);
					break;
				case IMAGETYPE_PNG:
					$source = imagecreatefrompng($sourceFile);
					break;
				case IMAGETYPE_GIF:
					$source = imagecreatefromgif($sourceFile);
					break;
			}
		}
		if (!empty($source))
		{
			$sw = $data[0];
			$sh = $data[1];
			if (empty($fit))
			{
				if ($height > 0)
				{
					$tw = $sw;
					$th = round($height / $width * $sw);
					if ($th > $sh)
					{
						$th = $sh;
						$tw = round($width / $height * $sh);
						$sx = round(($sw - $tw) / 2);
						$sy = 0;
					}
					else
					{
						$sx = 0;
						$sy = round(($sh - $th) / 2);
					}
					$target = $this->createImage($tw, $th, $data[2]);
					imagecopy($target, $source, 0, 0, $sx, $sy, $tw, $th);
					imagedestroy($source);
					$source = $target;
					$sw = $tw;
					$sh = $th;
				}
			}
			if ($width == 1)
			{
				$tw = round($sw / $height);
				$th = round($sh / $height);
			}
			elseif ($width > 0)
			{
				$tw = $width;
				$th = round($width / $sw * $sh);
				if ($height > 0)
				{
					if ($th > $height)
					{
						$th = $height;
						$tw = round($height / $sh * $sw);
					}
				}
			}
			else
			{
				$th = $height;
				$tw = round($height / $sh * $sw);
			}
			if (empty($fit) || $tw < $sw)
			{
				$target = $this->createImage($tw, $th, $data[2]);
				imagecopyresampled($target, $source, 0, 0, 0, 0, $tw, $th, $sw, $sh);
				imagedestroy($source);
			}
			else 
			{
				$target = $source;				
			}
			switch ($data[2])
			{
				case IMAGETYPE_JPEG:
					$result = imagejpeg($target, $targetFile, 85);
					break;
				case IMAGETYPE_PNG:
					$result = imagepng($target, $targetFile, 9);
					break;
				case IMAGETYPE_GIF:
					$result = imagegif($target, $targetFile);
					break;
			}
			imagedestroy($target);
		}
		return $result;
	}
	
	public function deleteThumbnail($filename)
	{
		$files = glob(self::DIR_THUMB."*/{$filename}");
		foreach ($files as $file) 
		{
			unlink($file);
		}
	}
	
}